package top.zuli.package01.class14;

public class WindowTest3 {
    public static void main(String[] args) {
        Runnable runnable = new Window3();
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        Thread t3 = new Thread(runnable);
        t1.start();
        t2.start();
        t3.start();
    }
}
class Window3 implements  Runnable{
    private int ticket = 100;
    private Object obj = new Object();
    @Override
    public void run() {
        while(true){
            //synchronized (obj) {
            synchronized (this) { //对
                if(ticket>0){
                    System.out.println(Thread.currentThread().getName()+"卖票,票号："+ticket);
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket--;
                } else {
                    break;
                }
            }
        }
    }
}
